<?php
namespace Admin\Controller;

class ShopController extends BaseController
{
    public function menu()
    {
        $menu = D("Menu")->getMenuList(array(), true);
        $this->assign("menu", $menu);
        $this->display();
    }

    public function product()
    {
//      每页显示的记录数
        $num = 25;
        $p = I("get.p") ? I("get.p") : 1;
        cookie("prevUrl", "Admin/Shop/product/p/" . $p);

        $productList = D("Product")->getProductList(array(), true, "id desc", $p, $num);
        $this->assign('productList', $productList);// 赋值数据集

        $count = D("Product")->getProductListCount();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, $num);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $show = $Page->show();// 分页显示输出

        $this->assign('page', $show);// 赋值分页输出
        $this->assign('url', "http://" . I("server.HTTP_HOST"));
        $this->display();
    }

    public function addMenu()
    {
        if (IS_POST) {
            D("Menu")->addMenu(I("post."));

            $this->success("保存成功", "Admin/Shop/menu");
        } else {
            $menuList = D("Menu")->getMenuList(array("pid" => 0));
            $this->assign("menuList", $menuList);
            $this->display();
        }
    }

    public function modifyMenu()
    {
        $menu = D("Menu")->getMenu(array("id" => I("get.id")), true);
        $this->assign("menu", $menu);

        $menuList = D("Menu")->getMenuList(array("pid" => 0));
        $this->assign("menuList", $menuList);

        $this->display("Shop:addMenu");
    }

    public function delMenu()
    {
        D("Menu")->delMenu(I("get.id"));

        $this->success("删除成功", "Admin/Shop/menu");
    }

    public function addProduct()
    {
        if (IS_POST) {
            $attrs = array();
            foreach (I("post.attrsName") as $key => $value) {
                if ($value == "") {
                    continue;
                }

                $attr = array();
                $attr["id"] = $key;
                $attr["name"] = $value;
                $attr["price"] = I("post.attrsPrice")[$key];
                array_push($attrs, $attr);
            }
            $attrs = json_encode($attrs);

            $albums = array();
            foreach (I("post.albums") as $key => $value) {
                if ($value) {
                    array_push($albums, $value);
                }
            }
            $albums = json_encode($albums);

//            过滤空对象
            if (strlen($attrs) == 2) {
                $attrs = "";
            }

            if (strlen($albums) == 2) {
                $albums = "";
            }

            $data = I("post.");
            $data['detail'] = I("post.detail", '', '');

            D("Product")->addProduct($data, $attrs, $albums);

            $this->success("保存成功", cookie("prevUrl"));
        } else {
            $menuList = D("Menu")->getMenuList();
            $this->assign("menuList", $menuList);
            $this->display();
        }
    }

    public function modifyProduct()
    {
        $product = D("Product")->getProduct(array("id" => I("get.id")), true);
        $this->assign("product", $product);

        $menuList = D("Menu")->getMenuList();
        $this->assign("menuList", $menuList);

        $this->display("Shop:addProduct");
    }

    public function updateProduct()
    {
        $data = I("get.");
        $id = $data["id"];
        unset($data["id"]);
        $data =  array("status" => I("get.status"));
        D("Product")->updateProduct($id, $data);

        $this->success("保存成功", cookie("prevUrl"));
    }


    public function delProduct()
    {
        D("Product")->delProduct(I("get.id"));

        $this->success("删除成功", cookie("prevUrl"));
    }

    public function ads()
    {
        $num = 25;
        $p = I("get.p") ? I("get.p") : 1;
        cookie("prevUrl", "Admin/Shop/ads/p/" . $p);

        $adsList = D("Ads")->getAdsList(array(), true, "id desc", $p, $num);
        $this->assign('ads', $adsList);// 赋值数据集

        $count = D("Ads")->getAdsListCount();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, $num);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $show = $Page->show();// 分页显示输出

        $this->assign('page', $show);// 赋值分页输出
        $this->display();
    }

    public function addAds()
    {
        if (IS_POST) {
            D("Ads")->addAds(I("post."));

            $this->success("保存成功", cookie("prevUrl"));
        } else {
            $this->display();
        }
    }

    public function modifyAds()
    {
        $ads = D("Ads")->getAds(array("id" => I("get.id")), true);
        $this->assign("ads", $ads);

        $this->display("Shop:addAds");
    }

    public function delAds()
    {
        D("Ads")->delAds(I("get.id"));

        $this->success("删除成功", cookie("prevUrl"));
    }

    public function comment()
    {
        $num = 25;
        $p = I("get.p") ? I("get.p") : 1;
        cookie("prevUrl", "Admin/Shop/comment/p/" . $p);

        $comment = D("Comment")->getCommentList(array(), true, "id desc", $p, $num);
        $this->assign("comment", $comment);

        $count = D("Comment")->getCommentListCount();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, $num);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $show = $Page->show();// 分页显示输出

        $this->assign('page', $show);// 赋值分页输出
        $this->display();
    }

    public function delComment()
    {
        D("Comment")->delComment(I("get.id"));

        $this->success("删除成功", cookie("prevUrl"));
    }

    public function productSearch()
    {
        $condition = array();
        if (I("post.id")) {
            array_push($condition, array("id" => I("post.id")));
        }
        if (I("post.name")) {
            array_push($condition, array("name" => array("like", array("%" . I("post.name") . "%", "%" . I("post.name"), I("post.name") . "%"), 'OR')));
        }
        if (I("post.recommend") != -10) {
            array_push($condition, array("recommend" => I("post.recommend")));
        }
        if (I("post.status") != -10) {
            array_push($condition, array("status" => I("post.status")));
        }
        if (I("post.timeRange")) {
            $timeRange = I("post.timeRange");
            $timeRange = explode(" --- ", $timeRange);
            array_push($condition, array("time" => array('between', array($timeRange[0], $timeRange[1]))));
        }

        $productList = D("Product")->getProductList($condition, true);

        $this->assign("productPost", I("post."));
        $this->assign("productList", $productList);
        $this->display("product");
    }

    public function exportProduct()
    {
        $product = D('Product')->getProductList(array(), true);
        foreach ($product as $key => $value) {
            unset($product[$key]["comment"]);
        }
        Vendor("PHPExcel.Excel#class");
        \Excel::export($product);
    }

    public function feedback()
    {
        $num = 25;
        $p = I("get.p") ? I("get.p") : 1;
        cookie("prevUrl", "Admin/Shop/feedback/p/" . $p);

        $feedbackList = D("Feedback")->getFeedbackList(array(), false, "id desc", $p, $num);
        $this->assign('feedback', $feedbackList);// 赋值数据集

        $count = D("Feedback")->getFeedbackListCount();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, $num);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $show = $Page->show();// 分页显示输出

        $this->assign('page', $show);// 赋值分页输出
        $this->display();
    }

    public function exportFeedback()
    {
        if (I("get.id")) {
            $feedback = D("Feedback")->getFeedbackList(array("id" => array("in", I("get.id"))));
        } else {
            $feedback = D("Feedback")->getFeedbackList();
        }

        Vendor("PHPExcel.Excel#class");
        \Excel::export($feedback);
    }

}